using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;
using System.Diagnostics;

namespace Sanchay
{
    public partial class Stock : Form
    {
        public Stock()
        {
            InitializeComponent();
        }

        private void Stock_Load(object sender, EventArgs e)
        {

            //ProgressWindow progress = new ProgressWindow();
            //progress.Text = "Work";
            //System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(DoSomeWork), progress);
            //progress.ShowDialog();

            DataTable dt = GetStockByPartsNo(txtPartsNo.Text);
            BindStockGrid(dt);
            //Thread t1 = new Thread(new ThreadStart(BindAndLoadGrid));
            //t1.Start();

            Thread t = new Thread(new ThreadStart(CalculateTotalStockValue));
            t.Start();

            decimal totalPrice = 0;

           
            NumberFormatInfo nfi = CultureInfo.GetCultureInfo("hi-IN").NumberFormat;
            nfi = (NumberFormatInfo)nfi.Clone();
            nfi.CurrencySymbol = "";
            nfi.CurrencyPositivePattern = 0;

            lblStockValue.Text = "Total Stock Value: ` " + string.Format(nfi, "{0:c}", totalPrice);

        }

        private void DoSomeWork(object status)
        {
            IProgressCallback callback = status as IProgressCallback;

            try
            {
                callback.Begin(0, 100);
                this.Invoke((MethodInvoker)delegate
                {
                    DataTable dt = GetStockByPartsNo(txtPartsNo.Text);
                    BindStockGrid(dt);

                  

                    decimal totalPrice = 0;
                    int i = 0;
                    while (i <= dataGridView1.Rows.Count - 1)
                    {
                        if (dataGridView1.Rows[i].Cells[4].Value != DBNull.Value)
                        {
                            totalPrice = totalPrice + Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
                        }
                        i += 1;
                    }
                    lblStockValue.Text = "Total Stock Value: " + totalPrice;
                    NumberFormatInfo nfi = CultureInfo.GetCultureInfo("hi-IN").NumberFormat;
                    nfi = (NumberFormatInfo)nfi.Clone();
                    nfi.CurrencySymbol = "";
                    nfi.CurrencyPositivePattern = 0;

                    lblStockValue.Text = "Total Stock Value: ` " + string.Format(nfi, "{0:c}", totalPrice);
                });
                if (callback.IsAborting)
                {
                    return;
                }
                System.Threading.Thread.Sleep(100);
                if (callback.IsAborting)
                {
                    return;
                }
                //for (int i = 0; i < 100; ++i)
                //{
                //    callback.SetText(String.Format("Performing op: {0}", i));
                //    callback.StepTo(i);
                //    if (callback.IsAborting)
                //    {
                //        return;
                //    }
                //    System.Threading.Thread.Sleep(100);
                //    if (callback.IsAborting)
                //    {
                //        return;
                //    }
                //}
            }
            catch (System.Threading.ThreadAbortException)
            {
                // We want to exit gracefully here (if we're lucky)
            }
            catch (System.Threading.ThreadInterruptedException)
            {
                // And here, if we can
            }
            finally
            {
                if (callback != null)
                {
                    callback.End();
                }
            }
        }
        private void BindAndLoadGrid()
        {
            this.Invoke((MethodInvoker)delegate
            {
                DataTable dt = GetStockByPartsNo(txtPartsNo.Text);
                BindStockGrid(dt);
            });
            
        }

        private void CalculateTotalStockValue()
        {
            decimal totalPrice = 0;
            this.Invoke((MethodInvoker)delegate
            {
                int i = 0;
                while (i <= dataGridView1.Rows.Count - 1)
                {
                    if (dataGridView1.Rows[i].Cells[4].Value != DBNull.Value)
                    {
                        totalPrice = totalPrice + Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
                    }
                    i += 1;
                }
                lblStockValue.Text = "Total Stock Value: " + totalPrice;
                NumberFormatInfo nfi = CultureInfo.GetCultureInfo("hi-IN").NumberFormat;
                nfi = (NumberFormatInfo)nfi.Clone();
                nfi.CurrencySymbol = "";
                nfi.CurrencyPositivePattern = 0;

                lblStockValue.Text = "Total Stock Value: ` " + string.Format(nfi, "{0:c}", totalPrice);
            });

        }


        private void BindStockGrid(DataTable dt)
        {
            
            dataGridView1.DataSource = dt.DefaultView;
            dataGridView1.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dataGridView1.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dataGridView1.Columns[3].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns[4].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns[5].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

            dataGridView1.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns[1].Width = 240;
            
            lblRecordCount.Text = " Total Records Found: " + dataGridView1.Rows.Count.ToString();
        }

        private DataTable GetStockByPartsNo(string partsNo)
        {
            DataAccess da = new DataAccess();
            string query = "SELECT * from vwCurrentStock WHERE PartsNo LIKE '" + txtPartsNo.Text + "%'";
            DataTable dt = da.SelectCommand(query);
            return dt;
        }

        private DataTable GetStockByPartsName(string partsName)
        {
            DataAccess da = new DataAccess();
            string query = "SELECT * from vwCurrentStock WHERE PartsName LIKE '%" + txtPartsName.Text + "%'";
            DataTable dt = da.SelectCommand(query);
            return dt;
        }
        private DataTable GetStockByInvoiceName(string invoiceNo)
        {
            DataAccess da = new DataAccess();
            string query = "SELECT * from vwCurrentStock WHERE InvoiceNo LIKE '%" + txtPartsName.Text + "%'";
            DataTable dt = da.SelectCommand(query);
            return dt;
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            { 
                if (dataGridView1.Rows[i].Cells[2].Value.ToString().Contains("-"))
                {
                    dataGridView1.Rows[i].DefaultCellStyle.ForeColor=Color.Red;
                }
            }

        }

        private void txtPartsNo_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = GetStockByPartsNo(txtPartsNo.Text);
            BindStockGrid(dt);
        }

        private void txtPartsName_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = GetStockByPartsName(txtPartsName.Text);
            BindStockGrid(dt);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = GetStockByInvoiceName(txtInvoiceNo.Text);
            BindStockGrid(dt);
        }

        //private DataTable GetStockByInvoiceName(string p)
        //{
        //    throw new Exception("The method or operation is not implemented.");
        //}



    }
}